﻿#if NET35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kernelx.Common.DataAnnotations
{

    /// <summary>
    /// 
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
    public class MaxLengthAttribute : ValidationAttribute
    {

        /// <summary>
        /// 
        /// </summary>
        public int Length { get; }

        /// <summary>
        /// 
        /// </summary>
        public MaxLengthAttribute(int length) => Length = length;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null && value.ToString().Length > Length)
                return new ValidationResult($"的长度不能大于{Length}个字符");
            return null;
        }
    }

}

#endif